package com.kr.aia.powerrp.db;

import java.util.ArrayList;
import java.util.List;


import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.widget.Toast;

public class ClosingDBUtil {

	// Database fields
	private SQLiteDatabase database;
	private MySQLiteHelper dbHelper;
	private Context context;
	
	private String[] allColumns = { MySQLiteHelper.COLUMN_CLOSING_ID, 
									MySQLiteHelper.COLUMN_CLOSING_TITLE, 
									MySQLiteHelper.COLUMN_CLOSING_CONTENT
								   };
	
	public ClosingDBUtil(Context context) {
		dbHelper = new MySQLiteHelper(context);
		this.context = context;
	}

	public void open() throws SQLException {
		database = dbHelper.getWritableDatabase();
	}

	public void close() {
		dbHelper.close();
	}
	
	
	public ClosingInfoVO insertClosingInfo(ClosingInfoVO closingInfo) {
		
		ContentValues values = new ContentValues();
		values.put(MySQLiteHelper.COLUMN_CLOSING_TITLE, closingInfo.getTitle());
		values.put(MySQLiteHelper.COLUMN_CLOSING_CONTENT, closingInfo.getContent());
		
		long insertId = database.insert(MySQLiteHelper.TABLE_CLOSING, null, values);
		
		// To show how to query
		Cursor cursor = database.query(MySQLiteHelper.TABLE_CLOSING,
				allColumns, MySQLiteHelper.COLUMN_CLOSING_ID + " = " + insertId, null, null, null, null);
		cursor.moveToFirst();
		
		return cursorToClosingInfo(cursor);
	}
	
	public long updateClosingData(ClosingInfoVO closingInfo){
		ContentValues values = new ContentValues();
		values.put(MySQLiteHelper.COLUMN_CLOSING_TITLE, closingInfo.getTitle());
		values.put(MySQLiteHelper.COLUMN_CLOSING_CONTENT, closingInfo.getContent());
		
		long insertId = database.update(MySQLiteHelper.TABLE_CLOSING, values, MySQLiteHelper.COLUMN_CLOSING_ID + " = " + closingInfo.getSeqno(), null);
		
		return insertId;
	}
	
	public long updateClosingTitle(ClosingInfoVO closingInfo){
		ContentValues values = new ContentValues();
		values.put(MySQLiteHelper.COLUMN_CLOSING_TITLE, closingInfo.getTitle());
		
		long insertId = database.update(MySQLiteHelper.TABLE_CLOSING, values, MySQLiteHelper.COLUMN_CLOSING_ID + " = " + closingInfo.getSeqno(), null);
		
		return insertId;
	}
	
	public long updateClosingContent(int idx, String content){
		ContentValues values = new ContentValues();
		values.put(MySQLiteHelper.COLUMN_CLOSING_CONTENT, content);
		
		long insertId = database.update(MySQLiteHelper.TABLE_CLOSING, values, MySQLiteHelper.COLUMN_CLOSING_ID + " = " + (idx+1), null);
		
		Log.e("PRP", MySQLiteHelper.COLUMN_CLOSING_ID + " = " + (idx+1));
		return insertId;
	}
	
	
	public void deleteClosingInfo(){
		database.delete(MySQLiteHelper.TABLE_CLOSING, null, null);
	}
	

	public ArrayList<ClosingInfoVO> getAllData() {
		ArrayList<ClosingInfoVO> closingList = new ArrayList<ClosingInfoVO>();
		
		ClosingInfoVO closingInfo = null;
		Cursor cursor = database.query(MySQLiteHelper.TABLE_CLOSING, allColumns, null, null, null, null, null);

		if(cursor.getCount() > 0){
			while(cursor.moveToNext()){
				closingInfo = cursorToClosingInfo(cursor);
				closingList.add(closingInfo);
			}
		}
		cursor.close();
		
		return closingList;
	}

	private ClosingInfoVO cursorToClosingInfo(Cursor cursor) {
		ClosingInfoVO closingInfo = new ClosingInfoVO();
		
		closingInfo.setSeqno(cursor.getInt(0));
		closingInfo.setTitle(cursor.getString(1));
		closingInfo.setContent(cursor.getString(2));
		
		return closingInfo;
	}
}